/**
 * 
 */
package au.edu.swin.videoshop.publicapp.ui.sections;

import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import au.edu.swin.videoshop.bean.Customer;
import au.edu.swin.videoshop.bean.Rental;
import au.edu.swin.videoshop.bean.Reservation;
import au.edu.swin.videoshop.service.CustomerService;
import au.edu.swin.videoshop.service.RentalService;
import au.edu.swin.videoshop.service.ReservationService;

public class UserSection implements Serializable {
	private String mUserName;
	private String mPassword;
	private Customer mCustomer;
	
	private List mReservations;
	private List mRentals;

	@EJB
	private CustomerService mCustomerService;

	@EJB
	private ReservationService mReservationService;

	@EJB
	private RentalService mRentalService;

	public UserSection() {
	}
	

	public Customer getCustomer() {
		return mCustomer;
	}
	
	protected void sortRentalsWithComparator(Comparator comparator) {
		Collections.sort(mRentals, comparator);
	}
	
	protected void sortReservationsWithComparator(Comparator comparator) {
		Collections.sort(mReservations, comparator);
	}

	public String sortRentalsByTitle() {
		sortRentalsWithComparator(new Rental.TitleNameComparator());
		return null;
	}

	public String sortRentalsByMediumType() {
		sortRentalsWithComparator(new Rental.MediumTypeComparator());
		return null;
	}
	
	public String sortRentalsByStore() {
		sortRentalsWithComparator(new Rental.StoreNameComparator());
		return null;
	}
	
	public String sortRentalsByDueDate() {
		sortRentalsWithComparator(new Rental.DueDateComparator());
		return null;
	}
	
	public String sortRentalsByReturnDate() {
		sortRentalsWithComparator(new Rental.ReturnDateComparator());
		return null;
	}
	
	public String sortRentalsByStatus() {
		sortRentalsWithComparator(new Rental.StatusComparator());
		return null;
	}
	
	public String sortReservationsByTitle() {
		sortReservationsWithComparator(new Reservation.TitleNameComparator());
		return null;
	}

	public String sortReservationsByMediumType() {
		sortReservationsWithComparator(new Reservation.MediumTypeComparator());
		return null;
	}
	
	public String sortReservationsByStore() {
		sortReservationsWithComparator(new Reservation.StoreNameComparator());
		return null;
	}
	
	public String sortReservationsByExpiryDate() {
		sortReservationsWithComparator(new Reservation.ExpiryDateComparator());
		return null;
	}

	
		
	public List getReservations() {
		if (mCustomer != null) {
			if (mReservations == null) {
				refreshReservations();
			}
			return mReservations;
		}

		return null;
	}
	
	public void refreshReservations() {
		if (mCustomer != null) {
			mReservations = mReservationService.findReservationsByCustomerId(mCustomer.getCustomerId().longValue());
		}
	}
	
	public List getRentals() {
		if (mCustomer != null) {
			if (mRentals == null) {
				mRentals = mRentalService.findRentalsByCustomerId(mCustomer.getCustomerId().longValue());
				sortRentalsByStatus();
			}
			return mRentals;
		}

		return null;
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return mUserName;
	}

	/**
	 * @param userName the userName to set
	 */
	public void setUserName(String userName) {
		mUserName = userName;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return mPassword;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		mPassword = password;
	}

	public String login() {
		mCustomer = mCustomerService.authenticate(mUserName, mPassword);
		mUserName = null;
		mPassword = null;
		return null;
	}
	
	public String logout() {
		FacesContext context = FacesContext.getCurrentInstance();
 		ExternalContext ec = context.getExternalContext();
 
 		HttpServletRequest request = (HttpServletRequest)ec.getRequest();
 	    request.getSession( false ).invalidate();
 	  
 	     
	     return "logout";
	}
}



